package com.gitee.somenvm.ninerpgitem.compat.attribute

import org.bukkit.Bukkit
import org.bukkit.entity.LivingEntity
import org.bukkit.inventory.ItemStack
import org.serverct.ersha.api.AttributeAPI
import org.serverct.ersha.api.event.AttrUpdateAttributeEvent
import java.util.*

/**
 * BY NineRpgItem
 * com.gitee.somenvm.ninerpgitem.compat.attribute
 *
 * @author  SomeNvM
 * @date  2022/12/12 21:58
 */
object AttributePlus3Bridge : AttributeBridge {

    override fun addAttributes(uuid: UUID, reads: List<String>) {
        addAttributes(UUID.randomUUID().toString(), uuid, reads)
    }

    override fun addAttributes(source: String, uuid: UUID, reads: List<String>) {
        val entity = Bukkit.getEntity(uuid) as? LivingEntity ?: error("null")
        AttributeAPI.addSourceAttribute(AttributeAPI.getAttrData(entity), source, reads)
    }

    override fun removeAttributes(uuid: UUID, source: String) {
        val entity = Bukkit.getEntity(uuid) as? LivingEntity ?: error("null")
        AttributeAPI.takeSourceAttribute(AttributeAPI.getAttrData(entity), source)
    }

    override fun update(uuid: UUID) {
        val entity = Bukkit.getEntity(uuid) as? LivingEntity ?: error("null")
        this.update(entity)
    }

    override fun update(entity: LivingEntity) {
        val data = AttributeAPI.getAttrData(entity)
        AttrUpdateAttributeEvent.After(data)
    }

    override fun getAttributeValue(uuid: UUID, keyword: String): Double {
        val entity = Bukkit.getEntity(uuid) as? LivingEntity ?: error("null")
        val serverKeyword = AttributeAPI.getDefaultAttributeName(keyword) ?: return 0.0
        return AttributeAPI.getAttrData(entity).getAttributeValue(serverKeyword).get(0).toDouble()
    }

    override fun getAttributeValue(itemStack: ItemStack, keyword: String): Number {
        val serverKeyword = AttributeAPI.getDefaultAttributeName(keyword) ?: return 0.0
        return AttributeAPI.getAttributeSource(itemStack).attributeData?.getAttributeValue(serverKeyword)?.get(0)
            ?.toDouble() ?: 0.0
    }

    override fun getAttributeValue(list: MutableList<String>, keyword: String): Number {
        val serverKeyword = AttributeAPI.getDefaultAttributeName(keyword) ?: return 0.0
        return AttributeAPI.getAttributeSource(list).attributeData?.getAttributeValue(serverKeyword)?.get(0)?.toDouble()
            ?: 0.0
    }
}